const {
  $Message
} = require("../../dist/base/index");
import api from "../../utils/apis.js";
import message from "../../utils/message.js";
import util from "../../utils/util.js"

Page({

  data: {
    //平台算力
    platformPowerInfo: {},
    isPlatformMaskShow: false,
    //项目算力
    projectPowerInfo: {},
    isProjectMaskShow: false,
    //设备总数
    minerInfo: {},
    isMinerMaskShow: false
  },

  onLoad: function(options) {
    let self = this;

    self.getPlatform();
    self.getProject();
    self.getDevice()
  },

  onPullDownRefresh: function() {
    let self = this;

    self.getPlatform();
    self.getProject();
    self.getDevice()
  },

  getPlatform() {
    let self = this;
    let token = wx.getStorageSync("authorization");

    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: api.platformApi,
      method: "get",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();
        if (res.statusCode == "200") {
          res.data.powerTotal = util.filterZero(res.data.powerTotal)

          self.setData({
            platformPowerInfo: res.data,
            isPlatformMaskShow: false
          })
        } else {
          if (res.data.title == 'Unauthorized') {
            wx.redirectTo({
              url: '/pages/login/login'
            })
            return
          } else if (res.data.title == '参数异常' && res.data.message == '用户未参与此类型项目') {
            self.setData({
              platformPowerInfo: {},
              isPlatformMaskShow: true
            })
          } else {
            message.showInfo(
              res.data.message ? res.data.message : "网络连接异常"
            );
          }
        }
      }
    })
  },

  getProject() {
    let self = this;
    let token = wx.getStorageSync("authorization");

    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: api.projectApi,
      method: "get",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();
        if (res.statusCode == "200") {
          res.data.projectPower = util.filterZero(res.data.projectPower)

          self.setData({
            projectPowerInfo: res.data,
            isProjectMaskShow: false
          })
        } else {
          if (res.data.title == 'Unauthorized') {
            wx.redirectTo({
              url: '/pages/login/login'
            })
            return
          } else if (res.data.title == '参数异常' && res.data.message == '用户未参与此类型项目') {
            self.setData({
              projectPowerInfo: {},
              isProjectMaskShow: true
            })
          } else {
            message.showInfo(
              res.data.message ? res.data.message : "网络连接异常"
            );
          }
        }
      }
    });
  },

  getDevice() {
    let self = this;
    let token = wx.getStorageSync("authorization");

    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: api.deviceApi,
      method: "get",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();
        wx.stopPullDownRefresh()

        if (res.statusCode == "200") {
          self.setData({
            minerInfo: res.data,
            isMinerMaskShow: false
          })
        } else {
          if (res.data.title == 'Unauthorized') {
            wx.redirectTo({
              url: '/pages/login/login'
            })
            return
          } else if (res.data.title == '参数异常' && res.data.message == '用户未参与此类型项目') {
            self.setData({
              minerInfo: {},
              isMinerMaskShow: true
            })
          } else {
            message.showInfo(
              res.data.message ? res.data.message : "网络连接异常"
            );
          }
        }
      }
    });
  },

  //页面导航
  toMiners() {
    wx.navigateTo({
      url: '/pages/miners/minerList'
    })
  },

  toPower() {
    wx.navigateTo({
      url: '/pages/power/power'
    })
  }
})